package com.example.mvvmtemplate.page.login;

import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.example.mvvmtemplate.R;
import com.example.mvvmtemplate.base.viewmodel.BaseMVVMActivity;
import com.example.mvvmtemplate.bean.LoginInfoBean;
import com.example.mvvmtemplate.page.main.MainActivity;
import com.example.mvvmtemplate.utli.edit.AlphabetReplaceMethod;
import com.example.mvvmtemplate.utli.edit.filter.EnglishNumberFilter;
import com.example.mvvmtemplate.model.LoginViewModel;

public class LoginActivity extends BaseMVVMActivity<LoginViewModel> {

    @Override
    public Class<LoginViewModel> getViewModelClass() {
        return LoginViewModel.class;
    }

    @Override
    public int getLayoutResID() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EditText editTextPhone = findViewById(R.id.editTextPhone);
        EditText editTextTextPassword = findViewById(R.id.editTextTextPassword);
        Button btLogin = findViewById(R.id.bt_login);

        EnglishNumberFilter englishNumberFilter = new EnglishNumberFilter();
        editTextPhone.setFilters(new InputFilter[]{englishNumberFilter});
        editTextPhone.setTransformationMethod(new AlphabetReplaceMethod());
        //登录
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editTextPhone.getText().toString();
                String password = editTextTextPassword.getText().toString();
                viewModel.login(name, password, 1);
            }
        });
        //登录回调
        viewModel.getLoginLiveData().observe(this, new Observer<LoginInfoBean>() {
            @Override
            public void onChanged(LoginInfoBean loginInfoBean) {
                startActivity(MainActivity.class);
                finish();
            }
        });
        viewModel.getThrowableLiveData().observe(this, throwable -> {
            showToast(throwable.getMessage());
        });
    }

}
